package com.crrc.dataparse.monitor.rsp.service.parse.treat.templatepara.datatrans;

import com.crrc.dataparse.templatepara.IDataTrans;
import java.text.DecimalFormat;

/*
    2进制转Double高端在前
 */
public class HexToDouble extends IDataTrans {

    @Override
    public String trans(String value, double resolution, double offset) throws Exception {
        long hex = binToOct(value);
        double d = Double.longBitsToDouble(hex);
        //String format = String.valueOf(resolution);
        //format = format.substring(0, format.length() - 1) + "0";
        String format = "0.000000";
        DecimalFormat df = new DecimalFormat(format);
        String result = df.format(d);
        return result;
    }
    static long binToOct(String bin){
        long total=0;
        char [] binCharArray=bin.toCharArray();
        for (int i = 0; i < binCharArray.length; i++) {
            total=total+binCharArray[i]-48;
            if(i!=binCharArray.length-1){
                total=total<<1;
            }
        }
        return total;
    }
}